package bean.managed;

import entity.InternalMessageEntity;
import entity.SystemUserEntity;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import managedbean.AbstractBacking;
import type.MessageType;

@ManagedBean(name = "messageController")
@SessionScoped
public class MessageController extends AbstractBacking implements Serializable {

    @EJB
    public bean.session.InternalMessageEntityFacade messageFacade;
    @EJB
    public bean.session.DepartmentEntityFacade departmentFacade;
    List<InternalMessageEntity> userMessageSent = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> userMessageInbox = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> announcementSent = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> announcementInbox = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> systemMessageSent = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> systemMessageInbox = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> draft = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> userMessageDraft = new ArrayList<InternalMessageEntity>();
    List<InternalMessageEntity> announcementDraft = new ArrayList<InternalMessageEntity>();
    SystemUserEntity logginUser;

    public MessageController() {
    }

    private void retrieveAnnouncementInbox() {
        logginUser = getCurrentUser();
        announcementInbox = messageFacade.getAnnouncementInboxForUser(logginUser);
    }
    
    private void retrieveSystemMessageInbox() {
        logginUser = getCurrentUser();
        systemMessageInbox = messageFacade.getSystemMessageInboxForUser(logginUser);
    }
    
    private void retrieveUserMessageInbox() {
        logginUser = getCurrentUser();
        userMessageInbox = messageFacade.getUserMessageInboxForUser(logginUser);
    }

    private void retrieveMessagesSent() {
        logginUser = getCurrentUser();
        userMessageSent = new ArrayList<InternalMessageEntity>();
        announcementSent = new ArrayList<InternalMessageEntity>();
        systemMessageSent = new ArrayList<InternalMessageEntity>();
        List<InternalMessageEntity> allMessagesSent = new ArrayList<InternalMessageEntity>();
        allMessagesSent.addAll(logginUser.getMessagesSent());
        for (InternalMessageEntity message : allMessagesSent) {
            if (message.getMessageType() == MessageType.UserMessage) {
                userMessageSent.add(message);
            } else if (message.getMessageType() == MessageType.Announcement) {
                announcementSent.add(message);
            } 
        }
        System.out.println("sent: u, a, s: " + userMessageSent.size() + ", "
                + announcementSent.size() + ", " + systemMessageSent.size());

    }

    private void retrieveDraft() {
        draft = messageFacade.getDraftForUser(getCurrentUser());
        announcementDraft = new ArrayList<InternalMessageEntity>();
        userMessageDraft = new ArrayList<InternalMessageEntity>();
        for (InternalMessageEntity d : draft) {
            if (d.getMessageType() == MessageType.Announcement) {
                announcementDraft.add(d);
            } else if (d.getMessageType() == MessageType.UserMessage) {
                userMessageDraft.add(d);
            }
        }
    }

    public List<InternalMessageEntity> getUserMessageSent() {
        Collections.sort(userMessageSent);
        return userMessageSent;
    }

    public List<InternalMessageEntity> getAnnouncementSent() {
        Collections.sort(announcementSent);
        return announcementSent;
    }

    public List<InternalMessageEntity> getSystemMessageSent() {
        Collections.sort(systemMessageSent);
        return systemMessageSent;
    }

    public List<InternalMessageEntity> getUserMessageInbox() {
        Collections.sort(userMessageInbox);
        return userMessageInbox;
    }

    public List<InternalMessageEntity> getSystemMessageInbox() {
        Collections.sort(systemMessageInbox);
        return systemMessageInbox;
    }

    public List<InternalMessageEntity> getAnnouncementInbox() {
        Collections.sort(announcementInbox);
        return announcementInbox;
    }

    public List<InternalMessageEntity> getDraft() {
        return draft;
    }

    public List<InternalMessageEntity> getAnnouncementDraft() {
        Collections.sort(announcementDraft);
        return announcementDraft;
    }

    public void setAnnouncementDraft(List<InternalMessageEntity> announcementDraft) {
        this.announcementDraft = announcementDraft;
    }

    public List<InternalMessageEntity> getUserMessageDraft() {
        Collections.sort(userMessageDraft);
        return userMessageDraft;
    }

    public void setUserMessageDraft(List<InternalMessageEntity> userMessageDraft) {
        this.userMessageDraft = userMessageDraft;
    }

    public boolean isDisplayAnnoucementInbox() {
        retrieveAnnouncementInbox();
        return announcementInbox.size() > 0;
    }

    public boolean isDisplayUserMessageInbox() {
        retrieveUserMessageInbox();
        return userMessageInbox.size() > 0;
    }

    public boolean isDisplayAnnoucementSent() {
        retrieveMessagesSent();
        return announcementSent.size() > 0;
    }

    public boolean isDisplayUserMessageSent() {
        retrieveMessagesSent();
        return userMessageSent.size() > 0;
    }

    public boolean isDisplaySystemMessageSent() {
        retrieveMessagesSent();
        return systemMessageSent.size() > 0;
    }

    public boolean isDisplaySystemMessageInbox() {
        retrieveSystemMessageInbox();
        return systemMessageInbox.size() > 0;
    }

    public boolean isDisplayDraft() {
        retrieveDraft();
        return draft.size() > 0;
    }

    public boolean isDisplayUserMessageDraft() {
        retrieveDraft();
        return userMessageDraft.size() > 0;
    }

    public boolean isDisplayAnnouncementDraft() {
        retrieveDraft();
        return announcementDraft.size() > 0;
    }
}
